package cn.com.twoke.app.pin_dou_dou.core;

import cn.hutool.core.lang.Version;
import lombok.extern.slf4j.Slf4j;

import javax.swing.*;
import java.awt.*;
import java.awt.Desktop;
import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;

/**
 * 更新提示对话框
 */
@Slf4j
public class UpdateDialog {
    
    /**
     * 显示更新提示对话框
     * @param parent 父窗口
     * @param appName 应用程序名称
     * @param currentVersion 当前版本
     * @param updateInfo 更新信息
     * @param downloadCallback 下载回调
     */
    public static void showUpdateDialog(JFrame parent, String appName, Version currentVersion,
                                       UpdateInfo updateInfo, UpdateDownloader.DownloadCallback downloadCallback) {
        // 创建对话框
        JDialog dialog = new JDialog(parent, "发现新版本", true);
        dialog.setLayout(new BorderLayout(10, 10));
        dialog.setResizable(false);
        
        // 创建主面板
        JPanel mainPanel = new JPanel(new BorderLayout(10, 10));
        mainPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
        
        // 创建顶部信息面板
        JPanel topPanel = new JPanel(new BorderLayout(5, 5));
        
        // 标题
        JLabel titleLabel = new JLabel(
            String.format("发现 %s 的新版本!", appName),
            SwingConstants.CENTER
        );
        titleLabel.setFont(titleLabel.getFont().deriveFont(Font.BOLD, 14f));
        topPanel.add(titleLabel, BorderLayout.NORTH);
        
        // 版本信息
        JLabel versionLabel = new JLabel(
            String.format("当前版本: %s -> 最新版本: %s", currentVersion, updateInfo.getVersion()),
            SwingConstants.CENTER
        );
        log.info("{}", titleLabel.getText());
        log.info("{}", versionLabel.getText());

        topPanel.add(versionLabel, BorderLayout.SOUTH);
        
        mainPanel.add(topPanel, BorderLayout.NORTH);
        
        // 创建描述面板
        if (updateInfo.getDescription() != null && !updateInfo.getDescription().isEmpty()) {
            JPanel descPanel = new JPanel(new BorderLayout());
            descPanel.setBorder(BorderFactory.createTitledBorder("更新内容"));
            
            JTextArea descArea = new JTextArea(updateInfo.getDescription());
            descArea.setEditable(false);
            descArea.setLineWrap(true);
            descArea.setWrapStyleWord(true);
            descArea.setFont(descArea.getFont().deriveFont(12f));
            descArea.setBackground(descPanel.getBackground());
            
            JScrollPane scrollPane = new JScrollPane(descArea);
            scrollPane.setPreferredSize(new Dimension(400, 150));
            descPanel.add(scrollPane, BorderLayout.CENTER);
            
            mainPanel.add(descPanel, BorderLayout.CENTER);
        }
        
        // 创建按钮面板
        JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT, 10, 10));
        
        // 下载按钮
        JButton downloadButton = new JButton("下载更新");
        downloadButton.addActionListener(e -> {
            dialog.dispose();
            // 启动下载
            UpdateDownloader.downloadUpdate(updateInfo.getDownloadUrl(), parent, downloadCallback);
        });
        
        // 在浏览器中打开按钮
        JButton openInBrowserButton = new JButton("在浏览器中打开");
        openInBrowserButton.addActionListener(e -> {
            try {
                Desktop.getDesktop().browse(new URI(updateInfo.getReleaseUrl()));
            } catch (IOException | URISyntaxException ex) {
                log.error("无法在浏览器中打开链接", ex);
                JOptionPane.showMessageDialog(
                    parent,
                    "无法在浏览器中打开链接: " + ex.getMessage(),
                    "错误",
                    JOptionPane.ERROR_MESSAGE
                );
            }
        });
        
        // 稍后提醒按钮
        JButton laterButton = new JButton("稍后提醒");
        laterButton.addActionListener(e -> dialog.dispose());
        
        buttonPanel.add(downloadButton);
        buttonPanel.add(openInBrowserButton);
        buttonPanel.add(laterButton);
        
        mainPanel.add(buttonPanel, BorderLayout.SOUTH);
        
        dialog.add(mainPanel, BorderLayout.CENTER);
        dialog.pack();
        dialog.setLocationRelativeTo(parent);
        dialog.setVisible(true);
    }
}